<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends Front_Controller{	
	protected $assignData = array();
	function __construct(){
		parent::__construct();
		$this->isLogin();
	}
	
	/*
	 * 个人中心首页 默认是订单
	 */
	public function index() {
		$this->load->view('home/order', $this->assignData);
	}
	
	/*
	 * 个人收藏
	 */
	public function collect() {
		$this->load->view('home/collect', $this->assignData);
	}
	
	/*
	 * 个人信息
	 */
	public function info() {
		$name = $this->session->userdata['name'];
		$post = $this->input->post();
		if (!empty($post)) {
			$tel = $post['tel'];
			$email = $post['email'];
			if ($email != $this->session->userdata['email']) {
				$userInfo = $this->JayphaDB->getDBResult($this->db_table_prefix.'users', array('email'=>$email));
				if (!empty($userInfo)) {
					$this->showmsg('邮箱地址已经存在！',site_url('home/info'));
				return;
				}
			}
			$address = $post['address'];
			$brief = $post['brief'];
			$updateArr = array(
				'tel' => $tel,
				'email' => $email,
				'address' => $address,
				'brief' => $brief,
			);
			$this->JayphaDB->updateDB($updateArr,$this->db_table_prefix.'users',array('name'=>$name));
			$this->session->set_userdata(array('email'=>$email));
			$this->showmsg('修改个人信息成功！',site_url('home/info'));
			return;
		}
		$user = $this->JayphaDB->getDBResult($this->db_table_prefix.'users', array('name'=>$name));
		$this->assignData = array_merge($user[0], $this->assignData);
		$this->load->view('home/info', $this->assignData);
	}
	
	/*
	 * 修改密码
	 */
	public function modify() {
		$post = $this->input->post();
		if (!empty($post)) {
			if (empty($post['old'])) {
				$this->showmsg('旧密码不能为空！');
				return;
			}
			if (empty($post['new'])) {
				$this->showmsg('新密码不能为空！');
				return;
			}
			
			if ($post['renew'] != $post['new']) {
				$this->showmsg('新密码和确认密码不一致！');
				return;
			}
			$name = $this->session->userdata['name'];
			//判断旧密码是否正确
			$this->load->library('SHA1', null, 'SHA1');
			$user = $this->JayphaDB->getDBResult($this->db_table_prefix.'users', array('name'=>$name, 'password'=>$this->SHA1->generate($post['old'])));
			if (empty($user)) {
				$this->showmsg('旧密码错误！');
				return;
			}
			
			$newPassword = $this->SHA1->generate($post['new']);
			$this->JayphaDB->updateDB(array('password'=>$newPassword),$this->db_table_prefix.'users',array('name'=>$name));
			$this->session->set_userdata(array('password'=>$password));
			$this->showmsg('修改密码成功！',site_url('home/index'));
			//@header('Location:'.site_url('home/index'));
			return;
		}
		$this->load->view('home/modify', $this->assignData);
	} 
	
	/*
	 * 判断是否登陆
	 */
	public function isLogin() {
		if (!isset($this->session->userdata['name']) || empty($this->session->userdata['name'])) {
			$this->showmsg('请先登陆！');
		}
	}
}
?>